package com.yunke.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunke.common.exception.BusinessException;
import com.yunke.entity.YunkeBanner;
import com.yunke.mapper.YunkeBannerMapper;
import com.yunke.service.YunkeBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 轮播图服务实现类
 * 
 * @author yunke
 * @since 1.0.0
 */
@Service
public class YunkeBannerServiceImpl extends ServiceImpl<YunkeBannerMapper, YunkeBanner> implements YunkeBannerService {

    @Autowired
    private YunkeBannerMapper bannerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createBanner(YunkeBanner banner) {
        if (StrUtil.isBlank(banner.getBannerTitle())) {
            throw new BusinessException("轮播图标题不能为空");
        }
        if (StrUtil.isBlank(banner.getBannerImage())) {
            throw new BusinessException("轮播图图片不能为空");
        }

        banner.setCreateTime(System.currentTimeMillis());
        banner.setUpdateTime(System.currentTimeMillis());
        banner.setStatus(0);

        // 设置默认值
        if (banner.getIsEnabled() == null) {
            banner.setIsEnabled(true);
        }
        if (banner.getTargetUser() == null) {
            banner.setTargetUser(1); // 全部用户
        }
        if (banner.getDisplayOrder() == null) {
            banner.setDisplayOrder(0);
        }
        if (banner.getClickCount() == null) {
            banner.setClickCount(0);
        }
        if (banner.getViewCount() == null) {
            banner.setViewCount(0);
        }

        return save(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBanner(YunkeBanner banner) {
        YunkeBanner existBanner = getById(banner.getId());
        if (existBanner == null) {
            throw new BusinessException("轮播图不存在");
        }

        banner.setUpdateTime(System.currentTimeMillis());
        return updateById(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBanner(Long id) {
        YunkeBanner banner = getById(id);
        if (banner == null) {
            throw new BusinessException("轮播图不存在");
        }

        banner.setStatus(1);
        banner.setUpdateTime(System.currentTimeMillis());
        return updateById(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBanners(List<Long> ids) {
        return bannerMapper.updateBannerStatus(ids, 1) > 0;
    }

    @Override
    public IPage<YunkeBanner> getBannerPage(Integer current, Integer size,
                                           Integer bannerType, String displayPosition,
                                           String bannerTitle, Boolean isEnabled) {
        Page<YunkeBanner> page = new Page<>(current, size);
        return bannerMapper.selectBannerPage(page, bannerType, displayPosition, bannerTitle, isEnabled);
    }

    @Override
    public YunkeBanner getBannerById(Long id) {
        return getById(id);
    }

    @Override
    public List<YunkeBanner> getBannersByPosition(String displayPosition, Integer targetUser, Boolean isEnabled) {
        Long currentTime = System.currentTimeMillis();
        return bannerMapper.selectBannersByPosition(displayPosition, targetUser, isEnabled, currentTime);
    }

    @Override
    public List<YunkeBanner> getBannersByType(Integer bannerType, Boolean isEnabled) {
        Long currentTime = System.currentTimeMillis();
        return bannerMapper.selectBannersByType(bannerType, isEnabled, currentTime);
    }

    @Override
    public List<YunkeBanner> getHomeBanners(Integer targetUser) {
        Long currentTime = System.currentTimeMillis();
        return bannerMapper.selectHomeBanners(targetUser, currentTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean enableBanner(Long id) {
        YunkeBanner banner = getById(id);
        if (banner == null) {
            throw new BusinessException("轮播图不存在");
        }

        banner.setIsEnabled(true);
        banner.setUpdateTime(System.currentTimeMillis());
        return updateById(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disableBanner(Long id) {
        YunkeBanner banner = getById(id);
        if (banner == null) {
            throw new BusinessException("轮播图不存在");
        }

        banner.setIsEnabled(false);
        banner.setUpdateTime(System.currentTimeMillis());
        return updateById(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBannerStatus(List<Long> ids, Integer status) {
        return bannerMapper.updateBannerStatus(ids, status) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateBannerEnabled(List<Long> ids, Boolean isEnabled) {
        return bannerMapper.updateBannerEnabled(ids, isEnabled) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateClickCount(Long id) {
        return bannerMapper.updateClickCount(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateViewCount(Long id) {
        return bannerMapper.updateViewCount(id) > 0;
    }

    @Override
    public long countBanners(Integer bannerType, String displayPosition, Boolean isEnabled) {
        return bannerMapper.countBanners(bannerType, displayPosition, isEnabled);
    }

    @Override
    public List<YunkeBanner> getExpiredBanners() {
        Long currentTime = System.currentTimeMillis();
        return bannerMapper.selectExpiredBanners(currentTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disableExpiredBanners() {
        List<YunkeBanner> expiredBanners = getExpiredBanners();
        if (expiredBanners.isEmpty()) {
            return true;
        }

        List<Long> ids = expiredBanners.stream()
                .map(YunkeBanner::getId)
                .toList();
        
        return updateBannerEnabled(ids, false);
    }
} 